import {Component, OnInit, Input} from '@angular/core';
import {Hero} from '../hero';
import {Location} from '@angular/common';
import {ActivatedRoute} from '@angular/router';
import {HeroService} from '../services/hero.service';

@Component({
  selector: 'app-hero-detail',
  templateUrl: './hero-detail.component.html',
  styleUrls: ['./hero-detail.component.css'],
  providers: []
})
export class HeroDetailComponent implements OnInit {

  @Input() hero: Hero;

  constructor(
    private location: Location,
    private route: ActivatedRoute,
    private heroService: HeroService
  ) {
  }

  ngOnInit() {
    this.getHero();
  }


  getHero(): void {
    const id = +this.route.snapshot.paramMap.get('id');
    this.heroService.getHero(id).subscribe(hero => this.hero = hero);

  }


  goBack(): void {
    this.location.back();
  }

  save(): void {
    this.heroService.updateHero(this.hero).subscribe(
      () => this.goBack()
    );
  }

}
